package br.usp.ime.origami.solver;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.util.List;

public class SolverCanvas extends Canvas {

    private static final int DEFAULT_WIDTH = 5;

    private final List<Part> parts;

    private final int L;
    
    private static final int LENGTH = 8;

    public SolverCanvas(List<Part> parts, int l) {
        this.parts = parts;
        this.L = l;
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        int startx = 100;
        int curx = startx, cury = 200;
        int orientation = 1;
        for (Part part : parts) {
            orientation *= part.getOrientation().getValue();
            g.setColor(orientation == 1 ? Color.BLUE : Color.RED);
            int len = part.getLength() * LENGTH * orientation;
            fillRect(g, curx, cury, DEFAULT_WIDTH * 8, DEFAULT_WIDTH * orientation);
            curx += DEFAULT_WIDTH * 6;
            cury += DEFAULT_WIDTH * orientation;
            fillRect(g, curx, cury, DEFAULT_WIDTH * 2, len);
            cury += len;
            fillRect(g, curx, cury, DEFAULT_WIDTH * 8, DEFAULT_WIDTH * orientation);
            cury += DEFAULT_WIDTH * orientation;
        }

        fillRect(g, curx, cury, DEFAULT_WIDTH * 8, DEFAULT_WIDTH * orientation);
        g.setColor(Color.BLACK);
        fillRect(g, curx, cury, 1, 1);
        curx += DEFAULT_WIDTH * 6;
        cury += DEFAULT_WIDTH * orientation;

        {
            orientation *= -1;
            g.setColor(orientation == 1 ? Color.BLUE : Color.RED);
            int len = (L * LENGTH + DEFAULT_WIDTH * 3) * orientation;
            fillRect(g, curx, cury, DEFAULT_WIDTH * 2, len);
            g.setColor(Color.BLACK);
            fillRect(g, curx, cury, 1, 1);
            cury += len;
        }

        g.setColor(Color.BLACK);
        fillRect(g, curx, cury, 1, 1);

        int w2 = curx - startx + DEFAULT_WIDTH * 3;
        {
            g.setColor(orientation == 1 ? Color.BLUE : Color.RED);
            fillRect(g, curx, cury, w2, 2 * DEFAULT_WIDTH * orientation);
            g.setColor(Color.BLACK);
            fillRect(g, curx, cury, 1, 1);
            cury += 2 * DEFAULT_WIDTH * orientation;
            curx += w2;
        }

        {
            orientation *= -1;
            g.setColor(orientation == 1 ? Color.BLUE : Color.RED);
            int len = (2 * L * LENGTH + 3 * DEFAULT_WIDTH) * orientation;
            fillRect(g, curx, cury, DEFAULT_WIDTH * 2, len);
            cury += len;
            curx += 2 * DEFAULT_WIDTH;
        }

        g.setColor(Color.BLACK);
        fillRect(g, curx, cury, 1, 1);

        {
            int w3 = w2 + DEFAULT_WIDTH * 2;
            g.setColor(orientation == 1 ? Color.BLUE : Color.RED);
            fillRect(g, curx - w3, cury, w3, 2 * DEFAULT_WIDTH * orientation);
            cury += 2 * DEFAULT_WIDTH * orientation;
            curx -= w3;
        }

    }

    private void fillRect(Graphics g, int x, int y, int w, int h) {
        if (h < 0) {
            y += h;
            h = -h;
        }
        // System.out.println(x + "," + (this.getHeight() - y - h) + " a " + (x
        // + w) + ","
        // + (this.getHeight() - y - h + h));
        g.fillRect(x, this.getHeight() - y - h, w, h);
    }

}
